package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.Mapper.ActivityMapper;
import com.one.Mapper.CategoryMapper;
import com.one.Mapper.ProductMapper;
import com.one.Mapper.ProductViewMapper;
import com.one.bean.Activity;
import com.one.bean.Category;
import com.one.bean.Product;
import com.one.bean.ProductView;
import com.one.service.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: tzy
 * @create: 2022/1/14
 * @FileName: HomePageServiceImpl
 * @Description: 主页展示业务接口实现类
 */
@Service
public class HomePageServiceImpl implements HomePageService {
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired(required = false)
    private ProductViewMapper productViewMapper;
    @Autowired(required = false)
    private ActivityMapper activityMapper;
    /**
     * @author: tzy
     * @date: 2022/1/14
     * @return: com.one.util.PageSupport<com.one.bean.Product>
     * @Description: 根据前端表单传来的商品分类查询相应的热销商品
     */
    @Override
    public List<Product> findHotProducts(Integer categoryFirst) {

        //查询product表中一级分类为categoryFirst的商品id的list集合
        //创建查询条件
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_first",categoryFirst).eq("purchase",0).eq("status",0).orderByDesc("sales_volume").last("limit 20");
        //List<Integer> ids = productMapper.selectList(queryWrapper).stream().map(Product::getId).collect(Collectors.toList());
        List<Product> hotProducts = productMapper.selectList(queryWrapper);

//        //如果ids为空，返回失败
//        if (ids != null && ids.size() != 0){
//            //根据商品的id集合查询视图里商品的信息，销量降序
//            //创建查询条件
//            QueryWrapper<ProductView> queryWrapper2 = new QueryWrapper<>();
//            queryWrapper2.in("id",ids).orderByDesc("sales_volume").last("limit 20");
//            List<ProductView> hotProducts = productViewMapper.selectList(queryWrapper2);
//            return hotProducts;
//        }
        return hotProducts;
    }

    /**
     * @author: tzy
     * @date: 2022/1/14
     * @return: java.util.List<com.one.bean.Category>
     * @Description: 根据商品一级分类，查询一二级分类的名称
     */
    @Override
    public List<Category> findCategories(Integer categoryFirst) {
        //查询一级分类下的二级分类集合
        //创建查询条件
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentid",categoryFirst);
        List<Category> categories = categoryMapper.selectList(queryWrapper);

        //将一级分类对象添加到集合里
        categories.add(categoryMapper.selectById(categoryFirst));

        return categories;
    }

    /**
     * @author: tzy
     * @date: 2022/1/17
     * @return: java.util.List<com.one.bean.Product>
     * @Description: 根据商品分类，新品展示
     */
    @Override
    public List<Product> findNewProducts(Integer categoryFirst) {
        //查询product表中一级分类为categoryFirst的商品id的list集合
        //创建查询条件
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_first",categoryFirst)
                .eq("purchase",0)
                .eq("status",0)
                .orderByDesc("create_Date")
                .last("limit 30");
        List<Product> newProducts = productMapper.selectList(queryWrapper);
        return newProducts;
    }


    /**
     * @author: tzy
     * @date: 2022/1/17
     * @return: java.util.List<com.one.bean.Activity>
     * @Description: 首页限时，秒杀，热销跳转
     */
    @Override
    public List<Activity> findActivities() {
        //查询活动表中所有活动的信息
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        return activityMapper.selectList(queryWrapper);
    }
}
